import cv2
import numpy as np

# 读取图像数据，第一份用于图像处理，第二份用于做匹配模板
stframe = cv2.imread("background.jpg")
st_temp = cv2.imread("background.jpg")
temp = np.asarray(stframe)


# 左右曲面的拼接
################################################################################
# 切左边圆弧上半段为裁剪，下半段为缝补
for i in range(3):
    for j in range(100):
        stframe[int(stframe.shape[0] / 2):, j, i] = 255
cv2.ellipse(stframe, center=(100, int(stframe.shape[0] / 4 + 50)), axes=(100, int(stframe.shape[0] / 4 - 50)), angle=0,
            startAngle=90, endAngle=270, color=(255, 255, 255), thickness=-1)
cv2.ellipse(stframe, center=(100, int(stframe.shape[0] / 4) * 3 - 50), axes=(100, int(stframe.shape[0] / 4 - 50)), angle=180,
            startAngle=90, endAngle=270, color=(255, 255, 255), thickness=-1)


for i in range(3):
    for j in range(100 + 1):
        for k in range(int(stframe.shape[0] / 2)):
            if st_temp[k, j, i] == stframe[k, j, i]:
                stframe[k, j, i] = 255
            else:
                stframe[k, j, i] = st_temp[k, j, i]
######################################################################################################
######################################################################################################
# 切右边的图像。下半段裁剪，上半段缝补
for i in range(3):
    for j in range(100):
        stframe[:int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i] = 255

cv2.ellipse(stframe, center=(stframe.shape[1] - 100, int(stframe.shape[0] / 4 + 50)), axes=(100, int(stframe.shape[0] / 4 - 50)),
            angle=0, startAngle=90, endAngle=270, color=(255, 255, 255), thickness=-1)
cv2.ellipse(stframe, center=(stframe.shape[1] - 100, (int(stframe.shape[0] / 4) * 3 - 50)), axes=(100, int(stframe.shape[0] / 4 - 50)),
            angle=180, startAngle=90, endAngle=270, color=(255, 255, 255), thickness=-1)

for i in range(3):
    for j in range(100):
        for k in range(int(stframe.shape[0] / 2)):
            if st_temp[k + int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i] == stframe[k + int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i]:
                stframe[k + int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i] = 255
            else:
                stframe[k + int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i] = st_temp[k + int(stframe.shape[0] / 2), stframe.shape[1] - 100 + j, i]

# ###########################################################################################################################################
#
# 为了像素对准，将左右处理后的图像进行备份
st_temp = stframe.copy()
# 上下曲面的拼接
# 同上的逻辑
for i in range(3):
    for j in range(100):
        stframe[j, :int(stframe.shape[1] / 2), i] = 255
cv2.ellipse(stframe, center=(int(stframe.shape[1] / 4 + 50), 100), axes=(int(stframe.shape[1] / 4) - 50, 100), angle=0,
            startAngle=0, endAngle=180, color=(255, 255, 255), thickness=-1)
cv2.ellipse(stframe, center=(int(stframe.shape[1] / 4) * 3 - 50, 100), axes=(int(stframe.shape[1] / 4) - 50, 100), angle=0,
            startAngle=180, endAngle=360, color=(255, 255, 255), thickness=-1)

for i in range(3):
    for j in range(100 + 1):
        for k in range(int(stframe.shape[1] / 2)):
            if st_temp[j, k + int(stframe.shape[1] / 2), i] == stframe[j, k + int(stframe.shape[1] / 2), i]:
                stframe[j, k + int(stframe.shape[1] / 2), i] = 255
            else:
                stframe[j, k + int(stframe.shape[1] / 2), i] = st_temp[j, k + int(stframe.shape[1] / 2), i]
#
for i in range(3):
    for j in range(100):
        stframe[stframe.shape[0] - 100 + j, 1000:, i] = 255
cv2.ellipse(stframe, center=(int(stframe.shape[1] / 4 + 50), stframe.shape[0] - 100), axes=(int(stframe.shape[1] / 4) - 50, 100),
            angle=0, startAngle=0, endAngle=180, color=(255, 255, 255), thickness=-1)
cv2.ellipse(stframe, center=(int(stframe.shape[1] / 4) * 3 - 50, stframe.shape[0] - 100), axes=(int(stframe.shape[1] / 4) - 50, 100),
            angle=0, startAngle=180, endAngle=360, color=(255, 255, 255), thickness=-1)

for i in range(3):
    for j in range(100):
        for k in range(int(stframe.shape[1] / 2)):
            if st_temp[stframe.shape[0] - 100 + j, k, i] == stframe[stframe.shape[0] - 100 + j, k, i]:
                stframe[stframe.shape[0] - 100 + j, k, i] = 255
            else:
                stframe[stframe.shape[0] - 100 + j, k, i] = st_temp[stframe.shape[0] - 100 + j, k, i]
# #######################################################################################################################
#
#
#
# #################################
# 拼上下三图
dst = np.zeros((stframe.shape[0] * 3 - 400, stframe.shape[1], 3), np.uint8)
for i in range(3):
    for j in range(stframe.shape[0]):
        for k in range(stframe.shape[1]):
            dst[j, k, i] = stframe[j, k, i]
for i in range(3):
    for j in range(stframe.shape[0] - 100):
        for k in range(int(stframe.shape[1])):
            if dst[stframe.shape[0] - 200 + j, k, i] == 255:
                dst[stframe.shape[0] - 200 + j, k, i] = stframe[j, k, i]
            elif dst[stframe.shape[0] - 200 + j, k, i] == 0:
                dst[stframe.shape[0] - 200 + j, k, i] = stframe[j, k, i]
for i in range(3):
    for j in range(100):
        for k in range(stframe.shape[1]):
            dst[stframe.shape[0] * 2 - 300 + j, k, i] = stframe[stframe.shape[0] - 100 + j, k, i]
#
for i in range(3):
    for j in range(stframe.shape[0] - 100):
        for k in range(int(stframe.shape[1])):
            if dst[stframe.shape[0] * 2 - 400 + j, k, i] == 255:
                dst[stframe.shape[0] * 2 - 400 + j, k, i] = stframe[j, k, i]
            elif dst[stframe.shape[0] * 2 - 400 + j, k, i] == 0:
                dst[stframe.shape[0] * 2 - 400 + j, k, i] = stframe[j, k, i]
for i in range(3):
    for j in range(100):
        for k in range(stframe.shape[1]):
            dst[dst.shape[0] - 100 + j, k, i] = stframe[stframe.shape[0] - 100 + j, k, i]
#
# ###########################################################################################
# # 拼3 * 3
res = np.zeros((stframe.shape[0] * 3 - 400, stframe.shape[1] * 3 - 400, 3), np.uint8)

for i in range(3):
    for j in range(dst.shape[0]):
        res[j, :stframe.shape[1], i] = dst[j, :, i]
for i in range(3):
    for j in range(dst.shape[1] - 100):
        for k in range(int(dst.shape[0])):
            if res[k, stframe.shape[1] - 200 + j, i] == 255:
                res[k, stframe.shape[1] - 200 + j, i] = dst[k, j, i]
            elif res[k, stframe.shape[1] - 200 + j, i] == 0:
                res[k, stframe.shape[1] - 200 + j, i] = dst[k, j, i]

for i in range(3):
    for j in range(100):
        for k in range(dst.shape[0]):
            res[k, dst.shape[1] * 2 - 300 + j, i] = dst[k, dst.shape[1] - 100 + j, i]


for i in range(3):
    for j in range(dst.shape[1] - 300):
        for k in range(int(dst.shape[0])):
            if res[k, stframe.shape[1] * 2 - 400 + j, i] == 255:
                res[k, stframe.shape[1] * 2 - 400 + j, i] = dst[k, j, i]
            elif res[k, stframe.shape[1] * 2 - 400 + j, i] == 0:
                res[k, stframe.shape[1] * 2 - 400 + j, i] = dst[k, j, i]

for i in range(3):
    for j in range(300):
        for k in range(dst.shape[0]):
            res[k, res.shape[1] - 300 + j, i] = dst[k, dst.shape[1] - 300 + j, i]

cv2.imwrite("reslut.jpg", res[500:500 + (stframe.shape[0] - 200) * 2, 500:500 + (stframe.shape[1] - 200) * 2, :])
cv2.imshow("temp", res[500:3232, 500:4100, :])
# # cv2.imshow("title", stframe)
# cv2.imshow("temp", res)
cv2.waitKey(0)